package com.jt.www.common.controller;

import com.jt.www.common.service.PolicyAttributeService;
import com.jt.www.model.policyattribute.vo.PlicyClassVO;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 保单属性字典表
 *
 * @author admin_ltf
 * @create 2019-06-17 15:52
 */
@Api(value = "保单属性字典表", tags = {"POLICY-ATTRIBUTE-API"}, description = "保单属性字典表")
@RestController
@RequestMapping(value = "/policy/attr")
public class PolicyAttributeController {

    public static final Logger log = LoggerFactory.getLogger(PolicyAttributeController.class);

    @Autowired
    PolicyAttributeService policyAttributeService;

    /**
     * 查询产品属性
     *
     * @param type
     * @param channel
     * @return
     */
    @ApiOperation(value = "查询保单属性字典表", notes = "查询保单属性字典表", httpMethod = "GET")
    @GetMapping(value = "/getInfo")
    public ResponseEntity getProInfo(@ApiParam(value = "属性类型.班级:0,年级:1,产品:2,产品类型:3,产品方案:4,学校类型:5,其他6", required = true) @RequestParam(value = "type", required = true) String type,
                                     @ApiParam(value = "教育渠道.我学保:wxb,教保网:edu,展业端:zyd", required = true) @RequestParam(value = "channel", required = true) String channel,
                                     @ApiParam(value = "父级id", required = false) @RequestParam(value = "pid", required = false) String pid) {
        if (StringUtils.isEmpty(type)) {
            log.error("【查询产品属性】type为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        if (StringUtils.isEmpty(channel)) {
            log.error("【查询产品属性】channel为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        List<PlicyClassVO> result = null;
        try {
            result = policyAttributeService.getProInfo(channel, type, pid);
        } catch (Exception e) {
            e.printStackTrace();
            return GenericDataResponse.errorWithMsg("查询失败");
        }

        return GenericDataResponse.okWithData(result);
    }

    @ApiOperation(value = "获取IP", notes = "获取IP", httpMethod = "GET")
    @GetMapping(value = "/getIp")
    public String getIpAddr() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }

}
